﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LiteQueryWPF.Controls
{
    /// <summary>
    /// FileRowPanel.xaml 的交互逻辑
    /// </summary>
    public partial class FileRowPanel : UserControl
    {
        public string ID
        {
            get { return Guid.NewGuid().ToString(); }
        }

        public event EventHandler OnRemoved;

        private FileInfo _fileInfo;
        public FileInfo FileInfo
        {
            get
            {
                return _fileInfo;
            }
            set
            {
                _fileInfo = value;
                if (_fileInfo != null)
                    txtFileName.Text = _fileInfo.Name.Substring(0, _fileInfo.Name.Length - _fileInfo.Extension.Length);
            }
        }

        public string FileName
        {
            get
            {
                return txtFileName.Text;
            }
        }

        public string FullFileName
        {
            get
            {
                if (_fileInfo != null)
                    return _fileInfo.FullName;
                else
                    return "";
            }
        }

        public string TableName
        {
            get
            {
                return txtTableName.Text;
            }
            set
            {
                txtTableName.Text = value;
            }
        }

        public int TypeValue
        {
            get
            {
                return cboType.SelectedIndex;
            }
        }

        public FileRowPanel()
        {
            InitializeComponent();
            InitCategory();
        }

        private void InitCategory()
        {
            this.cboType.Items.Add(0);
            this.cboType.Items.Add(1);
            this.cboType.Items.Add(2);
            this.cboType.Items.Add(3);
            this.cboType.Items.Add(4);
            this.cboType.Items.Add(5);
            this.cboType.Items.Add(6);
            this.cboType.Items.Add(7);
            this.cboType.Items.Add(8);
            this.cboType.Items.Add(9);

            this.cboType.SelectedIndex = 0;
            this.cboType.SelectionChanged += CboType_SelectedIndexChanged;
        }


        private void CboType_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(this.Parent); i++)
            {
                var child = VisualTreeHelper.GetChild(this.Parent, i) as FileRowPanel;
                if (child is null)
                    continue;
                if (child.ID == this.ID)
                    continue;
                if (child.TypeValue != this.TypeValue)
                    continue;
                // 0 默认不分类
                if (child.TypeValue == 0)
                    continue;
                this.TableName = child.TableName;
            }
        }

        public FileRowPanel(FileInfo fi)
        {
            InitializeComponent();
            _fileInfo = fi;
            txtFileName.Text = _fileInfo.Name.Substring(0, _fileInfo.Name.Length - _fileInfo.Extension.Length);
            txtTableName.Text = _fileInfo.Name.Substring(0, _fileInfo.Name.Length - _fileInfo.Extension.Length);

            InitCategory();
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if(this.Parent is StackPanel)
                (this.Parent as StackPanel).Children.Remove(this);
            else if (this.Parent is DockPanel)
                (this.Parent as DockPanel).Children.Remove(this);
            else if (this.Parent is WrapPanel)
                (this.Parent as WrapPanel).Children.Remove(this);
            else if (this.Parent is Grid)
                (this.Parent as Grid).Children.Remove(this);
            else if (this.Parent is Canvas)
                (this.Parent as Canvas).Children.Remove(this);

            if (OnRemoved != null)
                OnRemoved(this, null);
        }
    }
}
